import React from 'react';
import { inject, observer } from 'mobx-react';
import { WeaDialog, WeaRightMenu, WeaNewScroll, WeaFormItem, WeaSearchGroup, WeaSelect, WeaBrowser, WeaScope, WeaSelectGroup, WeaAuth } from 'ecCom';
import { Button, Row, Col } from 'antd';
import { WeaTableNew, WeaSwitch } from 'comsMobx';

const WeaTable = WeaTableNew.WeaTable;
import { toJS } from 'mobx';
import equal from 'deep-equal';
import _ from 'lodash';
import CrmShowGroup from '../crm-show-group';
import WeaCustomerShareTableEdit from '../WeaCrmTableEdit/customerShare.js';
import './index.less';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

class BatchShareDialog extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      datas: [],
    };
  }
  componentWillReceiveProps(nextProps) {

  }
  shouldComponentUpdate() {
    return true;
  }
  render() {
    const { visible, title, condition, onCancel } = this.props;
    const { datas } = this.state;
    const columns = [
      {
        title: getLabel(21956, '对象类型'),
        com: [{ label: '', type: 'INPUT', viewAttr: 1, key: 'sharetypespan', disabled: false }],
        width: '20%',
        dataIndex: 'sharetypespan',
        key: 'sharetypespan',
      },
      {
        title: getLabel(106, '对象'),
        com: [{ label: '', type: 'INPUT', viewAttr: 1, key: 'relatedshareidspan', disabled: false }],
        width: '30%',
        dataIndex: 'relatedshareidspan',
        key: 'relatedshareidspan',
      },
      {
        title: getLabel(683, '安全级别'),
        com: [{ label: '', type: 'INPUT', viewAttr: 1, key: 'seclevel_seclevelMax', disabled: false }],
        width: '30%',
        dataIndex: 'seclevel_seclevelMax',
        key: 'seclevel_seclevelMax',
      },
      {
        title: getLabel(3005, '共享级别'),
        com: [{ label: '', type: 'INPUT', viewAttr: 1, key: 'sharelevelspan', disabled: false }],
        width: '20%',
        dataIndex: 'sharelevelspan',
        key: 'sharelevelspan',
      },
    ];
    const that = this;
    return (
      <WeaDialog
        ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@ge4q4k`}
        title={title || getLabel(18645, '添加共享')}
        visible={visible}
        buttons={this.getBottomButtons(this.props.ecId || '')}
        icon="icon-coms-crm"
        iconBgcolor="#96358a"
        onCancel={() => { typeof onCancel === 'function' && onCancel(); }}
        style={{ width: 800, height: 500 }}
      >
        <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@4ba7tl`} datas={this.getRightMenu()} >
          <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@37juew`} height={'100%'}>
            <div style={{ width: '100%', height: '450px', overflow: 'auto' }}>
              <div >
                <CrmShowGroup ecId={`${this && this.props && this.props.ecId || ''}_CrmShowGroup@6rdhl6`} needTigger leftComponent={getLabel(1361, '基本信息')} showGroup >
                  <WeaAuth
                    ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@pogyvr`}
                    visible
                    ref="customerShare"
                    type="form"
                    className="crm-auth-customersize"
                    icon={'icon-coms-fa'}
                    iconBgcolor="#f14a2d"
                    conditions={condition}
                    title={getLabel(2112, '共享设置')}
                    onOk={(v) => {
                      if (typeof (v.seclevel) !== 'undefined') { v.seclevel_seclevelMax = `${v.seclevel}-${v.seclevelMax}`; }
                      const sharetype = v.sharetype;
                      if (sharetype == '3') {
                        const rolelevel = parseInt(v.rolelevel);
                        v.rolelevel = `${rolelevel - 1}`;
                      }
                      datas.push(v);
                      that.setState({ datas });
                    }}
                    onCancel={() => {}}
                  />
                </CrmShowGroup>
                <WeaCustomerShareTableEdit
                  ecId={`${this && this.props && this.props.ecId || ''}_WeaCustomerShareTableEdit@wdyi8o`}
                  title={getLabel(1279, '共享信息')}
                  showGroup
                  needAdd
                  needCopy={false}
                  columns={columns}
                  datas={datas}
                  canAdd
                  addColumns={this.addColumns.bind(this)}
                  onChange={this.editTableDatas.bind(this)}
                />
              </div>
            </div>
          </WeaNewScroll>
        </WeaRightMenu>
      </WeaDialog>
    );
  }
  getBottomButtons(ecId) {
    const { onCancel, onSave } = this.props;
    const { datas } = this.state;
    const btnArr = [];
    btnArr.push(<Button ecId={`${ecId}_Button@0iqfo5@save`} type="primary" onClick={() => { typeof onSave === 'function' && onSave(datas); this.setState({ datas: [] }); }}>{getLabel(86, '保存')}</Button>);
    btnArr.push(<Button ecId={`${ecId}_Button@ztms48@close`} type="primary" onClick={() => { typeof onCancel === 'function' && onCancel(); }}>{getLabel(309, '关闭')}</Button>);
    return btnArr;
  }
  getRightMenu() {
    const { onCancel, onSave } = this.props;
    const { datas } = this.state;
    const btns = [];
    	btns.push({
      key: '1',
    		icon: <i className="icon-coms-Preservation" />,
      content: getLabel(86, '保存'),
      onClick: () => { typeof onSave === 'function' && onSave(datas); this.setState({ datas: [] }); },
    	});
    	return btns;
  }
  addColumns() {
    this.refs.customerShare.onOk();
  }
  editTableDatas(data) {
    this.setState({ datas: data });
  }
}

export default BatchShareDialog;
